package com.laf.quartz.task;

import cn.hutool.core.collection.CollUtil;
import com.laf.manage.constant.LostItemStatusConstant;
import com.laf.manage.domain.po.LostItem;
import com.laf.manage.service.ILostItemService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 定时处理失物超时未提交的状态
 */
@Slf4j
@Component("lostItemTask")
@RequiredArgsConstructor
public class LostItemTask {

    private final ILostItemService lostItemService;

    /**
     * 每天凌晨一点处理超时未提交的失物
     */
    public void lostItemSubmitTimeout(){
        log.info("开始处理超时未提交的失物：");
        List<LostItem> itemList = lostItemService.lambdaQuery()
                .eq(LostItem::getStatus, LostItemStatusConstant.WAIT_SUBMIT)
                .list();
        if (CollUtil.isEmpty(itemList)) return;

        LocalDateTime now = LocalDateTime.now();
        // 如果失物超过三天没有提交就修改为状态超时
        List<Long> updateByIds = itemList.stream()
                .filter(item -> item.getCreateTime().plusDays(3).isBefore(now))
                .map(LostItem::getId)
                .collect(Collectors.toList());
        if (CollUtil.isEmpty(updateByIds)) return;

        lostItemService.lambdaUpdate()
                .set(LostItem::getStatus, LostItemStatusConstant.REJECTED)
                .set(LostItem::getUpdateTime, now)
                .set(LostItem::getUpdateBy, "10086")
                .set(LostItem::getRemark, "超时未提交")
                .in(LostItem::getId, updateByIds)
                .update();

    }


}
